<template>

  <div class="mod-home mod-demo-echarts">
    <p id="title-log">房源综合一览表</p>
    <el-row>
      <el-col span="6">
        <el-card>
          <div class="panel-body">
              <div id="main" style="height: 400px">
            </div>
          </div>
        </el-card>
      </el-col>
      <el-col span="6">
        <el-card>
          <div class="panel-body">
            <div id="mains" style="height: 400px">
            </div>
          </div>
        </el-card>
      </el-col>
      <el-col span="1">
        &nbsp

      </el-col>
      <el-col span="11">

        <el-form :inline="true" :model="dataForm" @keyup.enter.native="getDataList()">
          <el-form-item>
            <el-input v-model="dataForm.hostname" placeholder="户主姓名" clearable></el-input>
          </el-form-item>

          <el-form-item>
            <el-input v-model="dataForm.xiaoqu" placeholder="所属小区" clearable></el-input>
          </el-form-item>
          <el-form-item>
            <el-input v-model="dataForm.projectId" placeholder="所属项目" clearable></el-input>
          </el-form-item>
          <el-form-item>
            <el-button @click="ischaxun(),findHouse()">查询</el-button>
          </el-form-item>
          <el-form-item class="selects" label-position="right">
            <el-button @click="getDataList()" type="primary">展示所有房屋</el-button>
          </el-form-item>

          <el-form-item class="selects" label-position="right">
            <el-button @click="exportExcel()" type="success">导出</el-button>
          </el-form-item>
        </el-form>
        <el-table
          :data="dataList"
          border
          v-loading="dataListLoading"
          @selection-change="selectionChangeHandle"
          id="out-table"
          style="width: 100%;">
          <el-table-column
            prop="id"
            header-align="center"
            align="center"
            label="编号">
          </el-table-column>
          <el-table-column
            prop="huxing"
            header-align="center"
            align="center"
            label="户型">
          </el-table-column>
          <el-table-column
            prop="suqare"
            header-align="center"
            align="center"
            label="面积">
          </el-table-column>
          <el-table-column
            prop="xiaoqu"
            header-align="center"
            align="center"
            label="所属小区">
          </el-table-column>
          <el-table-column
            prop="dongId"
            header-align="center"
            align="center"
            label="所在楼号">
          </el-table-column>
          <el-table-column
            prop="houseNum"
            header-align="center"
            align="center"
            label="门牌号">
          </el-table-column>
          <el-table-column
            prop="projectName"
            header-align="center"
            align="center"
            label="项目归属">
          </el-table-column>
          <el-table-column
            prop="familyName"
            header-align="center"
            align="center"
            label="户主姓名">
          </el-table-column>
        </el-table>
        <el-pagination
          @size-change="sizeChangeHandle"
          @current-change="currentChangeHandle"
          :current-page="pageIndex"
          :page-sizes="[10, 20, 50, 100]"
          :page-size="pageSize"
          :total="totalPage"
          layout="total, sizes, prev, pager, next, jumper">
        </el-pagination>
      </el-col>
    </el-row>


  </div>
</template>

<script>
  import * as echarts from 'echarts'

  import FileSaver from 'file-saver'
  import XLSX from 'xlsx'

  export default {
    data () {
      return {
        chartPie: null,
        echarts: [],
        dataForm: {
          hostname: '',
          xiaoqu: '',
          projectId: ''
        },
        islist: true,
        isfenpei: true,
        isdiaobo: true,
        dataList: [],
        pageIndex: 1,
        pageSize: 10,
        totalPage: 0,
        dataListLoading: false,
        dataListSelections: [],
        addOrUpdateVisible: false,
        res: [],

        ischa: false
      }
    },
    activated () {
      // 由于给echart添加了resize事件, 在组件激活时需要重新resize绘画一次, 否则出现空白bug

      this.getDataList()
      this.feipeidiaobozonglan()
      this.gongchengyilan()
    },
    methods: {

      /*Excel导出相关*/
      exportExcel () {
        /* out-table关联导出的dom节点  */
        var wb = XLSX.utils.table_to_book(document.querySelector('#out-table'))
        /* get binary string as output */
        var wbout = XLSX.write(wb, { bookType: 'xlsx', bookSST: true, type: 'array' })
        try {
          FileSaver.saveAs(new Blob([wbout], { type: 'application/octet-stream' }), '房屋信息.xlsx')
        } catch (e) { if (typeof console !== 'undefined') console.log(e, wbout) }
        return wbout
      },


      feipeidiaobozonglan () {
        var chartDom = document.getElementById('main')
        var myChart = echarts.init(chartDom)
        var option
        var res = []
        option = {
          title: {
            text: '房源分配调拨总览',
            subtext: 'njit房源管理系统提供',
            left: 'center'
          },
          tooltip: {
            trigger: 'item'
          },
          legend: {
            orient: 'vertical',
            left: 'left',
          },
          color: ['#5470c6','#91cc75','#fac858'],

          series: [
            {
              type: 'pie',
              radius: '50%',
              data: res,
              emphasis: {
                itemStyle: {
                  shadowBlur: 10,
                  shadowOffsetX: 0,
                  shadowColor: 'rgba(0, 0, 0, 0.5)'
                }
              }
            }
          ]
        }
        this.$http({
          url: this.$http.adornUrl('/generator/echarts/demo1'),
          method: 'get',
        }).then(({data}) => {
          var length = data.echarts.length
          for (let i = 0; i<length; i++){
            res.push(data.echarts[i])
          }
          console.log(res)
          option && myChart.setOption(option)
        })
      },

      gongchengyilan(){
        var chartDom = document.getElementById('mains');
        var myChart = echarts.init(chartDom);
        var option;

        let xiaoqu = []

        let data1 = []
        let data2 = []
        let data3 = []

        option = {
          title: {
            text: '主要小区房源分配情况',
            subtext: 'njit房源管理系统提供',
            left: 'center'
          },
          tooltip: {
            trigger: 'axis',
            axisPointer: {            // Use axis to trigger tooltip
              type: 'shadow'        // 'shadow' as default; can also be 'line' or 'shadow'
            }
          },
        /*  legend: {
            data: ['Direct', 'Mail Ad', 'Affiliate Ad']
          },*/
          grid: {
            left: '3%',
            right: '4%',
            bottom: '3%',
            containLabel: true
          },
          xAxis: {
            type: 'value'
          },
          yAxis: {
            type: 'category',
            data: xiaoqu
          },
          color: ['#5470c6','#91cc75','#fac858'],
          series: [
            {
              name: '分配完成',
              type: 'bar',
              stack: 'total',
              label: {
                show: true
              },
              emphasis: {
                focus: 'series'
              },
              data: data1
            },
            {
              name: '已调拨未分配',
              type: 'bar',
              stack: 'total',
              label: {
                show: true
              },
              emphasis: {
                focus: 'series'
              },
              data: data2
            },
            {
              name: '未调拨',
              type: 'bar',
              stack: 'total',
              label: {
                show: true
              },
              emphasis: {
                focus: 'series'
              },
              data: data3
            }
          ]
        }

        this.$http({
          url: this.$http.adornUrl('/generator/echarts/mainxiaoqu'),
          method: 'get',
        }).then(({data}) => {
          if (data && data.code == 0){
            var xiaoqu1 = data.xiaoqu
            for (let i = 0; i < xiaoqu1.length; i++) {
              xiaoqu.push(xiaoqu1[i])
              data1.push(data.data1[i])
              data2.push(data.data2[i])
              data3.push(data.data3[i])
            }
          }
          option && myChart.setOption(option)
        })
      },
      // 获取数据列表
      getDataList () {
        if (this.dataForm.ischa) {
          this.pageSize = 10
          this.pageIndex = 1
        }
        this.islist = true
        this.dataListLoading = true
        this.$http({
          url: this.$http.adornUrl('/generator/house/listshow'),
          method: 'get',
          params: this.$http.adornParams({
            'page': this.pageIndex,
            'limit': this.pageSize

          })
        }).then(({data}) => {
          if (data && data.code === 0) {
            this.dataList = data.page.list

            this.totalPage = data.page.totalCount
          } else {
            this.dataList = []
            this.totalPage = 0
          }
          this.dataListLoading = false
          this.dataForm.ischa = false
        })
      },
      findHouse () {
        console.log('house')
        this.islist = false
        this.dataListLoading = true
        console.log(this.pageIndex)
        this.$http({
          url: this.$http.adornUrl('/generator/house/queryByHousePage'),
          method: 'get',
          params: this.$http.adornParams({
            'page': this.pageIndex,
            'limit': this.pageSize,
            'hostname': this.dataForm.hostname,
            'xiaoqu': this.dataForm.xiaoqu,
            'projectId': this.dataForm.projectId
          })
        }).then(({data}) => {
          if (data && data.code === 0) {
            this.dataList = data.page.list
            this.totalPage = data.page.totalCount
            console.log('if')
          } else {
            console.log('else')
            this.dataList = []
            this.totalPage = 0
          }
          this.dataListLoading = false
        })
      },
      //自定义查询标志位
      ischaxun () {

        this.dataForm.ischa = true

        console.log(this.dataForm.ischa)
      },

      // 每页数
      sizeChangeHandle (val) {
        this.pageSize = val
        this.pageIndex = 1
        if (this.islist) {
          this.getDataList()
        } else {
          this.findHouse()
        }
      },
      // 当前页
      currentChangeHandle (val) {
        this.pageIndex = val
        if (this.islist) {
          this.getDataList()
        } else {
          this.findHouse()
        }
      }
    }
  }

</script>
<style lang="scss">
  #title-log {
    font-size: 40px;
    margin-top: 10px;
    text-align: center;
  }

  .mod-demo-echarts {
    > .el-alert {
      margin-bottom: 10px;
    }

    > .el-row {
      margin-top: -10px;
      margin-bottom: -10px;

      .el-col {
        padding-top: 10px;
        padding-bottom: 10px;
      }
    }

    .chart-box {
      min-height: 400px;
    }
  }
</style>
